#include "slepui.h"
#include "cslep.h"
#include "slepbase.h"
#include <QtGui>



void SLEPUI::actglLeastR(){
	optsWidget = new OptionsWidget(Method_glLeast_R);
	LogWidget::instance()->log(QString("Executing Group Lasso"));
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		glLeastR(A,y,optsWidget->getL1RegCoefficient(),opts,x,funVal,valueL);
		LogWidget::instance()->log(QString("Success executing Group Lasso"));

		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = FileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);
		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemx,tr("x"));
		writeToCurrentDir(itemFVal,tr("funVal"));

		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}


void SLEPUI::actglLogisticR(){
	optsWidget = new OptionsWidget(Method_glLogistic_R);
	double c;
	LogWidget::instance()->log(QString("Group Lasso with Logistic Loss - Options"));
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);


		stopWatch timer;
		LogWidget::instance()->log(QString("Now running..."));
		::startTimer(&timer);
		glLogisticR(A,y,optsWidget->getL1RegCoefficient(),opts,x,&c,funVal,valueL);
		::stopTimer(&timer);
		LogWidget::instance()->log(QString("It took %1 seconds.").arg(getElapsedTime(&timer)));
		LogWidget::instance()->log(QString("Success executing Group Lasso with Logistic Loss!"));


		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = FileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);

		SLEPItem *itemC = new SLEPItem;

		itemC->singlevalue = c;
		itemC->type=SLEPItem::SINGLE_VALUE;
		writeToCurrentDir(itemC,tr("c"));

		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemx,tr("x"));
		writeToCurrentDir(itemFVal,tr("funVal"));

		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}